package com.eatme.page;

import com.eatme.dao.BasicListDao;
import com.eatme.dao.BasicListDataProvider;
import com.eatme.model.BasicList;
import org.apache.wicket.extensions.markup.html.repeater.data.table.DefaultDataTable;
import org.apache.wicket.extensions.markup.html.repeater.data.table.IColumn;
import org.apache.wicket.extensions.markup.html.repeater.data.table.filter.TextFilteredPropertyColumn;
import org.apache.wicket.markup.html.WebPage;
import org.apache.wicket.model.ResourceModel;
import org.apache.wicket.spring.injection.annot.SpringBean;

import java.util.Arrays;


/**
 * Homepage
 */

public class HomePage extends WebPage {

    @SpringBean(name = "basicListDao")
    private BasicListDao dao;

    private final DefaultDataTable<BasicList> users;

    /**
     * Constructor. Having this constructor public means that the page is
     * 'bookmarkable' and hence can be called/ created from anywhere.
     */
    public HomePage() {

        BasicListDataProvider dataProvider = new BasicListDataProvider(dao);

        // create the data table
        IColumn[] columns = createColumns();
        users = new DefaultDataTable("users", Arrays.asList(columns), dataProvider, 10);
        add(users);
    }

    private IColumn[] createColumns() {
        IColumn[] columns = new IColumn[3];

        columns[0] = createColumn("id", null, "id");
        columns[1] = createColumn("first.field", "firstField", "firstField");
        columns[2] = createColumn("second.field", "secondField", "secondField");
//        columns[2] = new ChoiceFilteredPropertyColumn(new ResourceModel("second.field"), "secondField",
//                "secondField", new LoadableDetachableModel() {
//            @Override
//            protected Object load() {
//                List<String> uniqueLastNames = dao.getUniqueLastNames();
//                uniqueLastNames.add(0, "");
//                return uniqueLastNames;
//            }
//        });

        return columns;
    }

    private TextFilteredPropertyColumn<String> createColumn(String key, String sortProperty,
                                                            String propertyExpression) {
        return new TextFilteredPropertyColumn<String>(new ResourceModel(key), sortProperty,
                propertyExpression);
    }

}
